﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CRM.Database.Model.Customers>" %>
<%@ Import Namespace="CRM.Database.Model" %>
<%@ Import Namespace="CRM.Resourses" %>




<% Html.DevExpress().GridView(settings =>
                                    {
                                        settings.Name = "gdAddresses";

                                        settings.KeyFieldName = "IdAddress";
                                        settings.SettingsPager.PageSize = 20;

                                        settings.CommandColumn.Visible = true;
                                        settings.CommandColumn.NewButton.Visible = true;
                                        settings.CommandColumn.DeleteButton.Visible = true;
                                         settings.CommandColumn.EditButton.Visible = true;
                                        settings.SettingsBehavior.ConfirmDelete = true;

                                        settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
                                        settings.SettingsText.PopupEditFormCaption = CRM.Resourses.Resourses.Edit;
                                        

                                        settings.CallbackRouteValues =
                                         new { Controller = "Customers", Action = "CustomerAddressesAndContacts_Addresses_Callback_Edit", customerId = Model.IdCustomer };

                                        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Customers", Action = "CustomerAddressesAndContacts_Addresses_Add", customerId = Model.IdCustomer };
                                        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Customers", Action = "CustomerAddressesAndContacts_Addresses_Update", customerId = Model.IdCustomer };
                                        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Customers", Action = "CustomerAddressesAndContacts_Addresses_Delete", customerId = Model.IdCustomer };
                                        
                                        settings.Columns.Add(column =>
                                                                 {
                                                                     column.Caption = Resourses.customerType;
                                                                     column.FieldName = "IdAddressesType";
                                                                     column.ColumnType = MVCxGridViewColumnType.ComboBox;
                                                                     var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                                                                     if (comboBoxProperties != null)
                                                                     {
                                                                         var col = new AddressesTypesCollection();
                                                                         comboBoxProperties.DataSource =col.AddressesTypesList;
                                                                         //collection.EquipmentTypes;
                                                                         comboBoxProperties.TextField = "LocalizedName";
                                                                         comboBoxProperties.ValueField = "IdAddressesType";
                                                                         comboBoxProperties.ValueType = typeof(int);
                                                                     }
                                                                 });
                                        settings.Columns.Add(column =>
                                                                 {
                                                                     column.Caption = Resourses.customerLegalEntity;
                                                                     column.FieldName = "LegalEntities.Name";
                                                                 });
                                        settings.Columns.Add(column =>
                                                                 {
                                                                     column.Caption = Resourses.customerZipCode;
                                                                     column.FieldName = "ZipCode";
                                                                 });
                                        settings.Columns.Add(column =>
                                                                 {
                                                                     column.Caption = Resourses.customerCountry;
                                                                     column.FieldName = "CountryNameLocalized";
                                                                 });
                                        settings.Columns.Add(column =>
                                        {
                                            column.Caption = Resourses.customerRegion;
                                            column.FieldName = "DistrictNameLocalized";
                                        });
                                        settings.Columns.Add(column =>
                                        {
                                            column.Caption = Resourses.customerDistrict;
                                            column.FieldName = "RegionNameLocalized";
                                        });
                                        settings.Columns.Add(column =>
                                                                 {
                                                                     column.Caption = Resourses.customerArea;
                                                                      column.FieldName = "AreasNameLocalized";
                                                                 });
                                        settings.Columns.Add(column =>
                                                                 {
                                                                     column.Caption = Resourses.customerCity;
                                                                     column.FieldName = "SettlementsLocalized";
                                                                  //   column.ColumnType =MVCxGridViewColumnType.DropDownEdit;
                                                                 });

                                        settings.Columns.Add(column =>
                                                                 {
                                                                     column.Caption = Resourses.customerStreet;
                                                                        column.FieldName = "Street";
                                                                 });
                                        settings.Columns.Add(column =>
                                                                 {
                                                                     column.Caption = Resourses.customerHouse;
                                                                        column.FieldName = "House";
                                                                 });
                                        settings.Columns.Add(column =>
                                                                 {
                                                                     column.Caption = Resourses.customerPhone;
                                                                     column.FieldName = "Phone";
                                                                 });
                                        settings.Columns.Add(column =>
                                                                 {
                                                                     column.Caption = Resourses.customerFax;
                                                                     column.FieldName = "Fax";
                                                                 });

                                        settings.Columns.Add(column =>
                                        {
                                            column.Caption = Resourses.Comments;
                                            column.FieldName = "Comments";
                                        });
                                        
                                        settings.Columns.Add(column =>
                                        {
                                            column.Caption = Resourses.IsPrimary;
                                            column.FieldName = "IsPrimary";
                                            column.ColumnType = MVCxGridViewColumnType.CheckBox;
                                        });

                                        settings.SetEditFormTemplateContent(c =>
                                                                                {
                                                                                    var address = c.DataItem;
                                                                                    var width = 200;
                                                                                    if (c.KeyValue == null)
                                                                                    {
                                                                                        var setCol =new SettlementsCollection();
                                                                                        address = new Addresses{IdAddress=-1};
                                                                                        ((Addresses) address).
                                                                                            IdAddressesItem = 1;// 152456;
                                                                                        ((Addresses) address).Settlements =(Settlements) setCol.GetItemById(1);
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        var col =new AddressesCollection();
                                                                                        address =
                                                                                            col.GetItemById(
                                                                                                Convert.ToInt32(
                                                                                                    c.KeyValue.ToString()));
                                                                                            
                                                                                            //DB.GetAddressById(Convert.ToInt32(c.KeyValue.ToString()));
                                                                                    }

                                                                                    // var address = ViewData["Address"] ?? c.DataItem;
                                                                                    ViewContext.Writer.Write("<table style='СustomerProfile'>");

                                                                                    ViewContext.Writer.Write("<tr><td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerType);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");


                                                                                    var addrCol =new AddressesTypesCollection();
                                                                                    
                                                                                    Html.DevExpress().ComboBox(s =>
                                                                                                                   {
                                                                                                                       s.Name = "IdAddressesType";
                                                                                                                       s.Properties.TextField = "LocalizedName";
                                                                                                                       s.Properties.ValueField = "IdAddressesType";
                                                                                                                       s.Properties.ValueType = typeof(int);
                                                                                                                       s.Width=new Unit(width);
                                                                                                                   }).BindList(addrCol.AddressesTypesList).Bind(DataBinder.Eval(address, "IdAddressesType")).GetHtml();//.IdAddressesType).GetHtml();


                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    //  ViewContext.Writer.Write("</tr><tr>");

                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerLegalEntity);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");

                                                                                    Html.DevExpress().ComboBox(s =>
                                                                                    {
                                                                                        s.Name = "IdLegalEntity";
                                                                                        s.Properties.TextField = "Name";
                                                                                        s.Properties.ValueField = "IdLegalEntity";
                                                                                        s.Properties.ValueType = typeof(int);
                                                                                        s.Width = new Unit(width);
                                                                                    }).BindList(Model.LegalEntities).Bind(DataBinder.Eval(address, "IdLegalEntity")).GetHtml();


                                                                                    ViewContext.Writer.Write("</td>");
                                                                                   // ViewContext.Writer.Write("</tr>");

                                                                                    ViewContext.Writer.Write("</tr><tr>");

                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerZipCode);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");

                                                                                    Html.DevExpress().TextBox(ss =>
                                                                                    { ss.Name = "ZipCode"; ss.Width = new Unit(width); }).Bind(DataBinder.Eval(address, "ZipCode")).GetHtml();


                                                                                    ViewContext.Writer.Write("</td>");

                                                                                    //ViewContext.Writer.Write("<td>");
                                                                                    //ViewContext.Writer.Write(Resourses.IsPrimary);
                                                                                    //ViewContext.Writer.Write("</td>");
                                                                                    //ViewContext.Writer.Write("<td>");

                                                                                    //Html.DevExpress().CheckBox(ss =>
                                                                                    //{ ss.Name = "IsPrimary"; }).Bind(DataBinder.Eval(address, "IsPrimary")).GetHtml();



                                                                                    //ViewContext.Writer.Write("</td>");
                                                                                    
                                                                                  //  ViewContext.Writer.Write("</tr>");
                                                                                    ViewContext.Writer.Write("</tr><tr>");

                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerCountry);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");



                                                                                    Html.RenderPartial("Country", DataBinder.Eval(address, "Settlements"));

                                                                                 //   Html.RenderPartial("Country", ((Addresses)address).Settlements);


                                                                                    ViewContext.Writer.Write("</td>");
                                                                                   // ViewContext.Writer.Write("</tr>");


                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerRegion);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");

                                                                                    //                                                                          
                                                                                    Html.RenderPartial("profineRegion", DataBinder.Eval(address, "Settlements"));

                                                                                    ViewContext.Writer.Write("</td>");
                                                                                  // ViewContext.Writer.Write("</tr>");

                                                                                    ViewContext.Writer.Write("</tr><tr>");

                                                                                   ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerDistrict);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");




                                                                                    //                                                                              

                                                                                    Html.RenderPartial("Region", DataBinder.Eval(address, "Settlements"));

                                                                                    ViewContext.Writer.Write("</td>");
                                                                                  //  ViewContext.Writer.Write("</tr>");

                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerCity);
                                                                                    ViewContext.Writer.Write("</td>");


                                                                                    ViewContext.Writer.Write("<td>");

                                                                                    Html.RenderPartial("City", DataBinder.Eval(address, "Settlements"));

                                                                                    ViewContext.Writer.Write("</td>");
                                                                                   // ViewContext.Writer.Write("</tr>");

                                                                                    ViewContext.Writer.Write("</tr><tr>");

                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerStreet);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");

                                                                                    Html.DevExpress().TextBox(ss =>
                                                                                    { ss.Name = "Street"; ss.Width = new Unit(width); }).Bind(DataBinder.Eval(address, "Street")).GetHtml(); 


                                                                                    ViewContext.Writer.Write("</td>");
                                                                                   // ViewContext.Writer.Write("</tr>");


                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerHouse);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");

                                                                                    Html.DevExpress().TextBox(ss =>
                                                                                    { ss.Name = "House"; ss.Width = new Unit(width); }).Bind(DataBinder.Eval(address, "House")).GetHtml();


                                                                                    ViewContext.Writer.Write("</td>");
                                                                                   // ViewContext.Writer.Write("</tr>");

                                                                                    ViewContext.Writer.Write("</tr><tr>");

                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerPhone);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");

                                                                                    Html.DevExpress().TextBox(ss =>
                                                                                    { ss.Name = "Phone"; ss.Width = new Unit(width); }).Bind(DataBinder.Eval(address, "Phone")).GetHtml();


                                                                                    ViewContext.Writer.Write("</td>");
                                                                                   // ViewContext.Writer.Write("</tr>");


                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.customerFax);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");

                                                                                    Html.DevExpress().TextBox(ss =>
                                                                                    { ss.Name = "Fax"; ss.Width = new Unit(width); }).Bind(DataBinder.Eval(address, "Fax")).GetHtml();


                                                                                    ViewContext.Writer.Write("</td>");
                                                                                  //  ViewContext.Writer.Write("</tr>");
                                                                                    ViewContext.Writer.Write("</tr><tr>");

                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.Comments);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");

                                                                                    Html.DevExpress().Memo(ss =>
                                                                                    { ss.Name = "Comments";
                                                                                        ss.Width = new Unit(335);
                                                                                    }).Bind(DataBinder.Eval(address, "Comments")).GetHtml();


                                                                                    ViewContext.Writer.Write("</td>");
                                                                                   // ViewContext.Writer.Write("</tr>");



                                                                                    ViewContext.Writer.Write("<td>");
                                                                                    ViewContext.Writer.Write(Resourses.IsPrimary);
                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");

                                                                                    Html.DevExpress().CheckBox(ss =>
                                                                                    { ss.Name = "IsPrimary"; }).Bind(DataBinder.Eval(address, "IsPrimary")).GetHtml();



                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("</tr>");

                                                                                    ViewContext.Writer.Write("<tr><td></td><td></td><td></td><td float='right'>");

                                                                                    ViewContext.Writer.Write(
                                                                                        "<table width='100%'><tr><td>");
                                                                                    Html.DevExpress().HyperLink(hsett =>
                                                                                    {
                                                                                        hsett.Name = "hyperlnk";
                                                                                        hsett.Properties.ClientSideEvents.Click = "function(s, e){ gdAddresses.UpdateEdit(); }";
                                                                                        // hsett.NavigateUrl = "#";
                                                                                        hsett.Properties.Text = Resourses.Save;
                                                                                    }).Render();


                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("<td>");


                                                                                    Html.DevExpress().HyperLink(hsett =>
                                                                                    {
                                                                                        hsett.Name = "hyperlsank";
                                                                                        hsett.Properties.ClientSideEvents.Click = "function(s, e){ gdAddresses.CancelEdit(); }";
                                                                                        //  hsett.NavigateUrl = "#";
                                                                                        hsett.Properties.Text = Resourses.Cancel;
                                                                                    }).Render();
                                                                                    ViewContext.Writer.Write(
                                                                                        "</td></tr></table>");


                                                                                    ViewContext.Writer.Write("</td>");
                                                                                    ViewContext.Writer.Write("</tr>");

                                                                                    ViewContext.Writer.Write("</table>");





                                                                                });
                                    }

    ).Bind(Model.Addresses).GetHtml();%>